home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Objects / object.c < prev    next >
C/C++ Source or Header  |  1998-05-30  |  18KB  |  863 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Generic object operations; and implementation of None (NoObject) */
  33.  
  34. #include "Python.h"
  35.  
  36. #include "protos/object_protos.h"
  37.  
  38. #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG )
  39. long _Py_RefTotal;
  40. #endif
  41.  
  42. /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros.
  43.    These are used by the individual routines for object creation.
  44.    Do not call them otherwise, they do not initialize the object! */
  45.  
  46. #ifdef COUNT_ALLOCS
  47. static PyTypeObject *type_list;
  48. extern int tuple_zero_allocs, fast_tuple_allocs;
  49. extern int quick_int_allocs, quick_neg_int_allocs;
  50. extern int null_strings, one_strings;
  51. void
  52. dump_counts()
  53. {
  54.     PyTypeObject *tp;
  55.  
  56.     for (tp = type_list; tp; tp = tp->tp_next)
  57.         fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n",
  58.             tp->tp_name, tp->tp_alloc, tp->tp_free,
  59.             tp->tp_maxalloc);
  60.     fprintf(stderr, "fast tuple allocs: %d, empty: %d\n",
  61.         fast_tuple_allocs, tuple_zero_allocs);
  62.     fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n",
  63.         quick_int_allocs, quick_neg_int_allocs);
  64.     fprintf(stderr, "null strings: %d, 1-strings: %d\n",
  65.         null_strings, one_strings);
  66. }
  67.  
  68. PyObject *
  69. get_counts()
  70. {
  71.     PyTypeObject *tp;
  72.     PyObject *result;
  73.     PyObject *v;
  74.  
  75.     result = PyList_New(0);
  76.     if (result == NULL)
  77.         return NULL;
  78.     for (tp = type_list; tp; tp = tp->tp_next) {
  79.         v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc,
  80.                   tp->tp_free, tp->tp_maxalloc);
  81.         if (v == NULL) {
  82.             Py_DECREF(result);
  83.             return NULL;
  84.         }
  85.         if (PyList_Append(result, v) < 0) {
  86.             Py_DECREF(v);
  87.             Py_DECREF(result);
  88.             return NULL;
  89.         }
  90.         Py_DECREF(v);
  91.     }
  92.     return result;
  93. }
  94.  
  95. void
  96. inc_count(tp)
  97.     PyTypeObject *tp;
  98. {
  99.     if (tp->tp_alloc == 0) {
  100.         /* first time; insert in linked list */
  101.         if (tp->tp_next != NULL) /* sanity check */
  102.             Py_FatalError("XXX inc_count sanity check");
  103.         tp->tp_next = type_list;
  104.         type_list = tp;
  105.     }
  106.     tp->tp_alloc++;
  107.     if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc)
  108.         tp->tp_maxalloc = tp->tp_alloc - tp->tp_free;
  109. }
  110. #endif
  111.  
  112. #ifndef MS_COREDLL
  113. PyObject *
  114. _PyObject_New(tp)
  115.     PyTypeObject *tp;
  116. #else
  117. PyObject *
  118. _PyObject_New(tp,op)
  119.     PyTypeObject *tp;
  120.     PyObject *op;
  121. #endif
  122. {
  123. #ifndef MS_COREDLL
  124.     PyObject *op = (PyObject *) malloc(tp->tp_basicsize);
  125. #endif
  126.     if (op == NULL)
  127.         return PyErr_NoMemory();
  128.     op->ob_type = tp;
  129.     _Py_NewReference(op);
  130.     return op;
  131. }
  132.  
  133. #ifndef MS_COREDLL
  134. PyVarObject *
  135. _PyObject_NewVar(tp, size)
  136.     PyTypeObject *tp;
  137.     int size;
  138. #else
  139. PyVarObject *
  140. _PyObject_NewVar(tp, size, op)
  141.     PyTypeObject *tp;
  142.     int size;
  143.     PyVarObject *op;
  144. #endif
  145. {
  146. #ifndef MS_COREDLL
  147.     PyVarObject *op = (PyVarObject *)
  148.         malloc(tp->tp_basicsize + size * tp->tp_itemsize);
  149. #endif
  150.     if (op == NULL)
  151.         return (PyVarObject *)PyErr_NoMemory();
  152.     op->ob_type = tp;
  153.     op->ob_size = size;
  154.     _Py_NewReference(op);
  155.     return op;
  156. }
  157.  
  158. int
  159. PyObject_Print(op, fp, flags)
  160.     PyObject *op;
  161.     FILE *fp;
  162.     int flags;
  163. {
  164.     int ret = 0;
  165.     if (PyErr_CheckSignals())
  166.         return -1;
  167.     if (op == NULL) {
  168.         fprintf(fp, "<nil>");
  169.     }
  170.     else {
  171.         if (op->ob_refcnt <= 0)
  172.             fprintf(fp, "<refcnt %u at %lx>",
  173.                 op->ob_refcnt, (long)op);
  174.         else if (op->ob_type->tp_print == NULL) {
  175.             if (op->ob_type->tp_repr == NULL) {
  176.                 fprintf(fp, "<%s object at %lx>",
  177.                     op->ob_type->tp_name, (long)op);
  178.             }
  179.             else {
  180.                 PyObject *s;
  181.                 if (flags & Py_PRINT_RAW)
  182.                     s = PyObject_Str(op);
  183.                 else
  184.                     s = PyObject_Repr(op);
  185.                 if (s == NULL)
  186.                     ret = -1;
  187.                 else if (!PyString_Check(s)) {
  188.                     PyErr_SetString(PyExc_TypeError,
  189.                            "repr not string");
  190.                     ret = -1;
  191.                 }
  192.                 else {
  193.                     fprintf(fp, "%s",
  194.                         PyString_AsString(s));
  195.                 }
  196.                 Py_XDECREF(s);
  197.             }
  198.         }
  199.         else
  200.             ret = (*op->ob_type->tp_print)(op, fp, flags);
  201.     }
  202.     if (ret == 0) {
  203.         if (ferror(fp)) {
  204.             PyErr_SetFromErrno(PyExc_IOError);
  205.             clearerr(fp);
  206.             ret = -1;
  207.         }
  208.     }
  209.     return ret;
  210. }
  211.  
  212. PyObject *
  213. PyObject_Repr(v)
  214.     PyObject *v;
  215. {
  216.     if (PyErr_CheckSignals())
  217.         return NULL;
  218.     if (v == NULL)
  219.         return PyString_FromString("<NULL>");
  220.     else if (v->ob_type->tp_repr == NULL) {
  221.         char buf[120];
  222.         sprintf(buf, "<%.80s object at %lx>",
  223.             v->ob_type->tp_name, (long)v);
  224.         return PyString_FromString(buf);
  225.     }
  226.     else
  227.         return (*v->ob_type->tp_repr)(v);
  228. }
  229.  
  230. PyObject *
  231. PyObject_Str(v)
  232.     PyObject *v;
  233. {
  234.     if (v == NULL)
  235.         return PyString_FromString("<NULL>");
  236.     else if (PyString_Check(v)) {
  237.         Py_INCREF(v);
  238.         return v;
  239.     }
  240.     else if (v->ob_type->tp_str != NULL)
  241.         return (*v->ob_type->tp_str)(v);
  242.     else {
  243.         PyObject *func;
  244.         PyObject *res;
  245.         if (!PyInstance_Check(v) ||
  246.             (func = PyObject_GetAttrString(v, "__str__")) == NULL) {
  247.             PyErr_Clear();
  248.             return PyObject_Repr(v);
  249.         }
  250.         res = PyEval_CallObject(func, (PyObject *)NULL);
  251.         Py_DECREF(func);
  252.         return res;
  253.     }
  254. }
  255.  
  256. static PyObject *
  257. do_cmp(v, w)
  258.     PyObject *v, *w;
  259. {
  260.     long c;
  261.     /* __rcmp__ actually won't be called unless __cmp__ isn't defined,
  262.        because the check in cmpobject() reverses the objects first.
  263.        This is intentional -- it makes no sense to define cmp(x,y)
  264.        different than -cmp(y,x). */
  265.     if (PyInstance_Check(v) || PyInstance_Check(w))
  266.         return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp);
  267.     c = PyObject_Compare(v, w);
  268.     if (c && PyErr_Occurred())
  269.         return NULL;
  270.     return PyInt_FromLong(c);
  271. }
  272.  
  273. int
  274. PyObject_Compare(v, w)
  275.     PyObject *v, *w;
  276. {
  277.     PyTypeObject *tp;
  278.     if (v == NULL || w == NULL) {
  279.         PyErr_BadInternalCall();
  280.         return -1;
  281.     }
  282.     if (v == w)
  283.         return 0;
  284.     if (PyInstance_Check(v) || PyInstance_Check(w)) {
  285.         PyObject *res;
  286.         int c;
  287.         if (!PyInstance_Check(v))
  288.             return -PyObject_Compare(w, v);
  289.         res = do_cmp(v, w);
  290.         if (res == NULL)
  291.             return -1;
  292.         if (!PyInt_Check(res)) {
  293.             Py_DECREF(res);
  294.             PyErr_SetString(PyExc_TypeError,
  295.                     "comparison did not return an int");
  296.             return -1;
  297.         }
  298.         c = PyInt_AsLong(res);
  299.         Py_DECREF(res);
  300.         return (c < 0) ? -1 : (c > 0) ? 1 : 0;    
  301.     }
  302.     if ((tp = v->ob_type) != w->ob_type) {
  303.         if (tp->tp_as_number != NULL &&
  304.                 w->ob_type->tp_as_number != NULL) {
  305.             int err;
  306.             err = PyNumber_CoerceEx(&v, &w);
  307.             if (err < 0)
  308.                 return -1;
  309.             else if (err == 0) {
  310.                 int cmp = (*v->ob_type->tp_compare)(v, w);
  311.                 Py_DECREF(v);
  312.                 Py_DECREF(w);
  313.                 return cmp;
  314.             }
  315.         }
  316.         return strcmp(tp->tp_name, w->ob_type->tp_name);
  317.     }
  318.     if (tp->tp_compare == NULL)
  319.         return (v < w) ? -1 : 1;
  320.     return (*tp->tp_compare)(v, w);
  321. }
  322.  
  323. long
  324. PyObject_Hash(v)
  325.     PyObject *v;
  326. {
  327.     PyTypeObject *tp = v->ob_type;
  328.     if (tp->tp_hash != NULL)
  329.         return (*tp->tp_hash)(v);
  330.     if (tp->tp_compare == NULL)
  331.         return (long) v; /* Use address as hash value */
  332.     /* If there's a cmp but no hash defined, the object can't be hashed */
  333.     PyErr_SetString(PyExc_TypeError, "unhashable type");
  334.     return -1;
  335. }
  336.  
  337. PyObject *
  338. PyObject_GetAttrString(v, name)
  339.     PyObject *v;
  340.     char *name;
  341. {
  342.     if (v->ob_type->tp_getattro != NULL) {
  343.         PyObject *w, *res;
  344.         w = PyString_InternFromString(name);
  345.         if (w == NULL)
  346.             return NULL;
  347.         res = (*v->ob_type->tp_getattro)(v, w);
  348.         Py_XDECREF(w);
  349.         return res;
  350.     }
  351.  
  352.     if (v->ob_type->tp_getattr == NULL) {
  353.         PyErr_Format(PyExc_AttributeError,
  354.                  "'%.50s' object has no attribute '%.400s'",
  355.                  v->ob_type->tp_name,
  356.                  name);
  357.         return NULL;
  358.     }
  359.     else {
  360.         return (*v->ob_type->tp_getattr)(v, name);
  361.     }
  362. }
  363.  
  364. int
  365. PyObject_HasAttrString(v, name)
  366.     PyObject *v;
  367.     char *name;
  368. {
  369.     PyObject *res = PyObject_GetAttrString(v, name);
  370.     if (res != NULL) {
  371.         Py_DECREF(res);
  372.         return 1;
  373.     }
  374.     PyErr_Clear();
  375.     return 0;
  376. }
  377.  
  378. int
  379. PyObject_SetAttrString(v, name, w)
  380.     PyObject *v;
  381.     char *name;
  382.     PyObject *w;
  383. {
  384.     if (v->ob_type->tp_setattro != NULL) {
  385.         PyObject *s;
  386.         int res;
  387.         s = PyString_InternFromString(name);
  388.         if (s == NULL)
  389.             return -1;
  390.         res = (*v->ob_type->tp_setattro)(v, s, w);
  391.         Py_XDECREF(s);
  392.         return res;
  393.     }
  394.  
  395.     if (v->ob_type->tp_setattr == NULL) {
  396.         if (v->ob_type->tp_getattr == NULL)
  397.             PyErr_SetString(PyExc_TypeError,
  398.                    "attribute-less object (assign or del)");
  399.         else
  400.             PyErr_SetString(PyExc_TypeError,
  401.                    "object has read-only attributes");
  402.         return -1;
  403.     }
  404.     else {
  405.         return (*v->ob_type->tp_setattr)(v, name, w);
  406.     }
  407. }
  408.  
  409. PyObject *
  410. PyObject_GetAttr(v, name)
  411.     PyObject *v;
  412.     PyObject *name;
  413. {
  414.     if (v->ob_type->tp_getattro != NULL)
  415.         return (*v->ob_type->tp_getattro)(v, name);
  416.     else
  417.         return PyObject_GetAttrString(v, PyString_AsString(name));
  418. }
  419.  
  420. int
  421. PyObject_HasAttr(v, name)
  422.     PyObject *v;
  423.     PyObject *name;
  424. {
  425.     PyObject *res = PyObject_GetAttr(v, name);
  426.     if (res != NULL) {
  427.         Py_DECREF(res);
  428.         return 1;
  429.     }
  430.     PyErr_Clear();
  431.     return 0;
  432. }
  433.  
  434. int
  435. PyObject_SetAttr(v, name, value)
  436.     PyObject *v;
  437.     PyObject *name;
  438.     PyObject *value;
  439. {
  440.     int err;
  441.     Py_INCREF(name);
  442.     PyString_InternInPlace(&name);
  443.     if (v->ob_type->tp_setattro != NULL)
  444.         err = (*v->ob_type->tp_setattro)(v, name, value);
  445.     else
  446.         err = PyObject_SetAttrString(
  447.             v, PyString_AsString(name), value);
  448.     Py_DECREF(name);
  449.     return err;
  450. }
  451.  
  452. /* Test a value used as condition, e.g., in a for or if statement.
  453.    Return -1 if an error occurred */
  454.  
  455. int
  456. PyObject_IsTrue(v)
  457.     PyObject *v;
  458. {
  459.     int res;
  460.     if (v == Py_None)
  461.         res = 0;
  462.     else if (v->ob_type->tp_as_number != NULL)
  463.         res = (*v->ob_type->tp_as_number->nb_nonzero)(v);
  464.     else if (v->ob_type->tp_as_mapping != NULL)
  465.         res = (*v->ob_type->tp_as_mapping->mp_length)(v);
  466.     else if (v->ob_type->tp_as_sequence != NULL)
  467.         res = (*v->ob_type->tp_as_sequence->sq_length)(v);
  468.     else
  469.         res = 1;
  470.     if (res > 0)
  471.         res = 1;
  472.     return res;
  473. }
  474.  
  475. /* equivalent of 'not v' 
  476.    Return -1 if an error occurred */
  477.  
  478. int
  479. PyObject_Not(v)
  480.     PyObject *v;
  481. {
  482.     int res;
  483.     res = PyObject_IsTrue(v);
  484.     if (res < 0)
  485.         return res;
  486.     return res == 0;
  487. }
  488.  
  489. /* Coerce two numeric types to the "larger" one.
  490.    Increment the reference count on each argument.
  491.    Return -1 and raise an exception if no coercion is possible
  492.    (and then no reference count is incremented).
  493. */
  494.  
  495. int
  496. PyNumber_CoerceEx(pv, pw)
  497.     PyObject **pv, **pw;
  498. {
  499.     register PyObject *v = *pv;
  500.     register PyObject *w = *pw;
  501.     int res;
  502.  
  503.     if (v->ob_type == w->ob_type && !PyInstance_Check(v)) {
  504.         Py_INCREF(v);
  505.         Py_INCREF(w);
  506.         return 0;
  507.     }
  508.     if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) {
  509.         res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw);
  510.         if (res <= 0)
  511.             return res;
  512.     }
  513.     if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) {
  514.         res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv);
  515.         if (res <= 0)
  516.             return res;
  517.     }
  518.     return 1;
  519. }
  520.  
  521. int
  522. PyNumber_Coerce(pv, pw)
  523.     PyObject **pv, **pw;
  524. {
  525.     int err = PyNumber_CoerceEx(pv, pw);
  526.     if (err <= 0)
  527.         return err;
  528.     PyErr_SetString(PyExc_TypeError, "number coercion failed");
  529.     return -1;
  530. }
  531.  
  532.  
  533. /* Test whether an object can be called */
  534.  
  535. int
  536. PyCallable_Check(x)
  537.     PyObject *x;
  538. {
  539.     if (x == NULL)
  540.         return 0;
  541.     if (x->ob_type->tp_call != NULL ||
  542.         PyFunction_Check(x) ||
  543.         PyMethod_Check(x) ||
  544.         PyCFunction_Check(x) ||
  545.         PyClass_Check(x))
  546.         return 1;
  547.     if (PyInstance_Check(x)) {
  548.         PyObject *call = PyObject_GetAttrString(x, "__call__");
  549.         if (call == NULL) {
  550.             PyErr_Clear();
  551.             return 0;
  552.         }
  553.         /* Could test recursively but don't, for fear of endless
  554.            recursion if some joker sets self.__call__ = self */
  555.         Py_DECREF(call);
  556.         return 1;
  557.     }
  558.     return 0;
  559. }
  560.  
  561.  
  562. /*
  563. NoObject is usable as a non-NULL undefined value, used by the macro None.
  564. There is (and should be!) no way to create other objects of this type,
  565. so there is exactly one (which is indestructible, by the way).
  566. */
  567.  
  568. /* ARGSUSED */
  569. static PyObject *
  570. none_repr(op)
  571.     PyObject *op;
  572. {
  573.     return PyString_FromString("None");
  574. }
  575.  
  576. static PyTypeObject PyNothing_Type = {
  577.     PyObject_HEAD_INIT(&PyType_Type)
  578.     0,
  579.     "None",
  580.     0,
  581.     0,
  582.     0,        /*tp_dealloc*/ /*never called*/
  583.     0,        /*tp_print*/
  584.     0,        /*tp_getattr*/
  585.     0,        /*tp_setattr*/
  586.     0,        /*tp_compare*/
  587.     (reprfunc)none_repr, /*tp_repr*/
  588.     0,        /*tp_as_number*/
  589.     0,        /*tp_as_sequence*/
  590.     0,        /*tp_as_mapping*/
  591.     0,        /*tp_hash */
  592. };
  593.  
  594. PyObject _Py_NoneStruct = {
  595.     PyObject_HEAD_INIT(&PyNothing_Type)
  596. };
  597.  
  598.  
  599. #ifdef Py_TRACE_REFS
  600.  
  601. static PyObject refchain = {&refchain, &refchain};
  602.  
  603. void
  604. _Py_ResetReferences()
  605. {
  606.     refchain._ob_prev = refchain._ob_next = &refchain;
  607.     _Py_RefTotal = 0;
  608. }
  609.  
  610. void
  611. _Py_NewReference(op)
  612.     PyObject *op;
  613. {
  614.     _Py_RefTotal++;
  615.     op->ob_refcnt = 1;
  616.     op->_ob_next = refchain._ob_next;
  617.     op->_ob_prev = &refchain;
  618.     refchain._ob_next->_ob_prev = op;
  619.     refchain._ob_next = op;
  620. #ifdef COUNT_ALLOCS
  621.     inc_count(op->ob_type);
  622. #endif
  623. }
  624.  
  625. void
  626. _Py_ForgetReference(op)
  627.     register PyObject *op;
  628. {
  629.     register PyObject *p;
  630.     if (op->ob_refcnt < 0)
  631.         Py_FatalError("UNREF negative refcnt");
  632.     if (op == &refchain ||
  633.         op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op)
  634.         Py_FatalError("UNREF invalid object");
  635. #ifdef SLOW_UNREF_CHECK
  636.     for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) {
  637.         if (p == op)
  638.             break;
  639.     }
  640.     if (p == &refchain) /* Not found */
  641.         Py_FatalError("UNREF unknown object");
  642. #endif
  643.     op->_ob_next->_ob_prev = op->_ob_prev;
  644.     op->_ob_prev->_ob_next = op->_ob_next;
  645.     op->_ob_next = op->_ob_prev = NULL;
  646. #ifdef COUNT_ALLOCS
  647.     op->ob_type->tp_free++;
  648. #endif
  649. }
  650.  
  651. void
  652. _Py_Dealloc(op)
  653.     PyObject *op;
  654. {
  655.     destructor dealloc = op->ob_type->tp_dealloc;
  656.     _Py_ForgetReference(op);
  657.     op->ob_type = NULL;
  658.     (*dealloc)(op);
  659. }
  660.  
  661. void
  662. _Py_PrintReferences(fp)
  663.     FILE *fp;
  664. {
  665.     PyObject *op;
  666.     fprintf(fp, "Remaining objects:\n");
  667.     for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) {
  668.         fprintf(fp, "[%d] ", op->ob_refcnt);
  669.         if (PyObject_Print(op, fp, 0) != 0)
  670.             PyErr_Clear();
  671.         putc('\n', fp);
  672.     }
  673. }
  674.  
  675. PyObject *
  676. _Py_GetObjects(self, args)
  677.     PyObject *self;
  678.     PyObject *args;
  679. {
  680.     int i, n;
  681.     PyObject *t = NULL;
  682.     PyObject *res, *op;
  683.  
  684.     if (!PyArg_ParseTuple(args, "i|O", &n, &t))
  685.         return NULL;
  686.     op = refchain._ob_next;
  687.     res = PyList_New(0);
  688.     if (res == NULL)
  689.         return NULL;
  690.     for (i = 0; (n == 0 || i < n) && op != &refchain; i++) {
  691.         while (op == self || op == args || op == res || op == t ||
  692.                t != NULL && op->ob_type != (PyTypeObject *) t) {
  693.             op = op->_ob_next;
  694.             if (op == &refchain)
  695.                 return res;
  696.         }
  697.         if (PyList_Append(res, op) < 0) {
  698.             Py_DECREF(res);
  699.             return NULL;
  700.         }
  701.         op = op->_ob_next;
  702.     }
  703.     return res;
  704. }
  705.  
  706. #endif
  707.  
  708.  
  709. /* Hack to force loading of cobject.o */
  710. PyTypeObject *_Py_cobject_hack = &PyCObject_Type;
  711.  
  712.  
  713. /* Hack to force loading of abstract.o */
  714. int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length;
  715.  
  716.  
  717. /* Malloc wrappers (see mymalloc.h) */
  718.  
  719. /* The Py_{Malloc,Realloc} wrappers call PyErr_NoMemory() on failure */
  720.  
  721. ANY *
  722. Py_Malloc(nbytes)
  723.     size_t nbytes;
  724. {
  725.     ANY *p;
  726. #if _PyMem_EXTRA > 0
  727.     if (nbytes == 0)
  728.         nbytes = _PyMem_EXTRA;
  729. #endif
  730.     p = malloc(nbytes);
  731.     if (p != NULL)
  732.         return p;
  733.     else {
  734.         PyErr_NoMemory();
  735.         return NULL;
  736.     }
  737. }
  738.  
  739. ANY *
  740. Py_Realloc(p, nbytes)
  741.     ANY *p;
  742.     size_t nbytes;
  743. {
  744. #if _PyMem_EXTRA > 0
  745.     if (nbytes == 0)
  746.         nbytes = _PyMem_EXTRA;
  747. #endif
  748.     p = realloc(p, nbytes);
  749.     if (p != NULL)
  750.         return p;
  751.     else {
  752.         PyErr_NoMemory();
  753.         return NULL;
  754.     }
  755. }
  756.  
  757. void
  758. Py_Free(p)
  759.     ANY *p;
  760. {
  761.     free(p);
  762. }
  763.  
  764. /* The PyMem_{Malloc,Realloc} wrappers don't call anything on failure */
  765.  
  766. ANY *
  767. PyMem_Malloc(nbytes)
  768.     size_t nbytes;
  769. {
  770. #if _PyMem_EXTRA > 0
  771.     if (nbytes == 0)
  772.         nbytes = _PyMem_EXTRA;
  773. #endif
  774.     return malloc(nbytes);
  775. }
  776.  
  777. ANY *
  778. PyMem_Realloc(p, nbytes)
  779.     ANY *p;
  780.     size_t nbytes;
  781. {
  782. #if _PyMem_EXTRA > 0
  783.     if (nbytes == 0)
  784.         nbytes = _PyMem_EXTRA;
  785. #endif
  786.     return realloc(p, nbytes);
  787. }
  788.  
  789. void
  790. PyMem_Free(p)
  791.     ANY *p;
  792. {
  793.     free(p);
  794. }
  795.  
  796.  
  797. /* These methods are used to control infinite recursion in repr, str, print,
  798.    etc.  Container objects that may recursively contain themselves,
  799.    e.g. builtin dictionaries and lists, should used Py_ReprEnter() and
  800.    Py_ReprLeave() to avoid infinite recursion.
  801.  
  802.    Py_ReprEnter() returns 0 the first time it is called for a particular
  803.    object and 1 every time thereafter.  It returns -1 if an exception
  804.    occurred.  Py_ReprLeave() has no return value.
  805.  
  806.    See dictobject.c and listobject.c for examples of use.
  807. */
  808.  
  809. #define KEY "Py_Repr"
  810.  
  811. int
  812. Py_ReprEnter(obj)
  813.     PyObject *obj;
  814. {
  815.     PyObject *dict;
  816.     PyObject *list;
  817.     int i;
  818.  
  819.     dict = PyThreadState_GetDict();
  820.     if (dict == NULL)
  821.         return -1;
  822.     list = PyDict_GetItemString(dict, KEY);
  823.     if (list == NULL) {
  824.         list = PyList_New(0);
  825.         if (list == NULL)
  826.             return -1;
  827.         if (PyDict_SetItemString(dict, KEY, list) < 0)
  828.             return -1;
  829.         Py_DECREF(list);
  830.     }
  831.     i = PyList_GET_SIZE(list);
  832.     while (--i >= 0) {
  833.         if (PyList_GET_ITEM(list, i) == obj)
  834.             return 1;
  835.     }
  836.     PyList_Append(list, obj);
  837.     return 0;
  838. }
  839.  
  840. void
  841. Py_ReprLeave(obj)
  842.     PyObject *obj;
  843. {
  844.     PyObject *dict;
  845.     PyObject *list;
  846.     int i;
  847.  
  848.     dict = PyThreadState_GetDict();
  849.     if (dict == NULL)
  850.         return;
  851.     list = PyDict_GetItemString(dict, KEY);
  852.     if (list == NULL || !PyList_Check(list))
  853.         return;
  854.     i = PyList_GET_SIZE(list);
  855.     /* Count backwards because we always expect obj to be list[-1] */
  856.     while (--i >= 0) {
  857.         if (PyList_GET_ITEM(list, i) == obj) {
  858.             PyList_SetSlice(list, i, i + 1, NULL);
  859.             break;
  860.         }
  861.     }
  862. }
  863.